package the_art_of_java_concurrency_programming.concurrent.Lock;

import org.junit.Test;
import the_art_of_java_concurrency_programming.concurrent.Thread.SleepUtils;

import java.util.concurrent.locks.Lock;

/**
 * @Author: 李浩然
 * @Date: 2021/5/24 4:21 下午
 */
public class TwinsLockTest {
    @Test
    public void testTwinsLock() {
        final Lock lock = new TwinsLock();
        class Worker extends Thread {
            public void run() {
                while (true) {
                    lock.lock();
                    try {
                        SleepUtils.second(1);
                        System.out.println(Thread.currentThread().getName());
                        SleepUtils.second(1);
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }
        for (int i = 0; i < 10; i++) {
            Worker w = new Worker();
            w.setDaemon(true);
            w.start();
        }

        //每隔一秒换行
        for (int i = 0; i < 10; i++) {
            SleepUtils.second(1);
            System.out.println();
        }
    }
}
